package com.fairytask.engine.components.comboBox
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.list.List;
	import com.fairytask.engine.components.list.ListEvent;
	import com.fairytask.engine.components.list.ListItem;
	import com.fairytask.engine.components.text.Label;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ComboBox extends BaseComponent
	{
		
		private var _titleContainer:Sprite;	//显示框容器	
		private var _titleItem:ListItem;			
		private var _listContainer:Sprite;		//下拉列表的容器	
		private var _list:List;		
		private var _listSkin:String="";			//下拉列表的皮肤
		private var _listHeight:Number;		//下拉列表的高
		private var _dataList:Array = [];		//下拉列表
    	private var _indexItem:int=-1;			//选中的Index，默认为 -1	
		private var _selectItem	:ListItem;
		private var _layOut		:String = "center";
		private var _initWid		:int;
		private var _hotArea		:Sprite;
		
		/**
		 *
		 * 创建下拉框 
		 * @param width 		宽
		 * @param height 		高
		 * @param left			对齐的偏移量
		 * @param listHeight	下拉列表的高
		 * @param txtSkinName	显示框的皮肤
		 * @param listSkinName	下拉列表的皮肤
		 * @param listBackName	下拉列表的背景
		 * @param dataList		数据	
		 * @param useScrollPanel滚动条
		 * @param layOut		列表项文本对齐方式，默认居中
		 */	
		public function ComboBox(   	width		:uint	=	50,
									  	height		:uint	=	26,
									  	left		:int	=	0,
									  	listHeight	:uint	=	100,
									  	txtSkinName:String	=	"",
									  	listSkinName:String	=	"",
									  	listBackName:String	=	"",
									  	dataList	:Array	=	null,
									  	useScrollPanel:Boolean=true,
										layOut		:String = 	"center"
										)
		{
			if(dataList == null)dataList = [];
			init();
			_listSkin = listSkinName;
			_listHeight = height;
			_layOut	  = layOut;
			_initWid	= width;
			createList(width,listHeight,useScrollPanel,dataList,listBackName,_listSkin,_layOut);
			createTitle(width,height,txtSkinName,dataList);
			drawHotArea(width,(height+listHeight));
			addListener();
		}
		
		/**
		 * 点击热区 
		 * 
		 */
		private function drawHotArea(w:int,h:int):void
		{
			_hotArea	=	new Sprite();
			addChildAt(_hotArea,0);
			_hotArea.graphics.beginFill(0xff0000,0);
			_hotArea.graphics.drawRect(0,0,w,h);
			_hotArea.graphics.endFill();
		}
		
		private function init():void
		{
			_titleContainer = new Sprite();
			this.addChild(_titleContainer);
			_listContainer = new Sprite();
			this.addChild(_listContainer);
			
			_listContainer.mouseChildren = _listContainer.mouseEnabled = _listContainer.visible = false;
		}
		/**
		 * 创建下拉列表 
		 * @param w					宽
		 * @param h					高
		 * @param useScrollPanel	滚动条
		 * @param dataList			数据
		 * @param backgroundSkin	背景皮肤
		 * 
		 */		
		private function createList(w:int,h:int,useScrollPanel:Boolean=true,dataList:Array=null,backgroundSkin:String="",itemSkin:String="",layOut:String="center"):void
		{
			_list = new List(w,h,useScrollPanel,toListData(dataList),backgroundSkin,itemSkin,layOut);
			_list.autoWidth	=	true;
			_listContainer.addChild(_list);
			
		}	
		
		/**
		 *创建显示框 
		 * @param width			宽
		 * @param height		高
		 * @param txtSkinName 	显示框的皮肤
		 * @param dataList		数据	
		 * 
		 */		
		private function createTitle(width:int,height:int,txtSkinName:String,dataList:Array):void
		{
			
			_titleItem = new ListItem("",width,height,txtSkinName);
			_titleContainer.addChild(_titleItem);
			_listContainer.y = _titleItem.height;
			initTitle(0);
		}	
		/**
		 * 下拉框的监听事件
		 * 
		 */		
		private function addListener():void
		{
			_titleContainer.addEventListener(MouseEvent.MOUSE_DOWN,showOrHideList);
			_list.addEventListener(ListEvent.LIST_CLICK,updataTitle);
//			this.addEventListener(MouseEvent.ROLL_OUT,hideList);
		}
		
		/**
		 *给dataList添加数据  
		 * @param dataList
		 * @return 
		 * 
		 */		
		private function toListData(pList:Array):Array
		{
			_dataList = dataList;
			
			var recList:Array = [];
			for each(var data:Object in dataList){
				recList.push({label:data["label"],height:_listHeight,skin:_listSkin});
			}
			return recList;
		}
		/**
		 *显示框中显示选中的数据 
		 * @param index
		 * 
		 */		
		private function initTitle(index:int):void
		{	
			var titleStr:String;
			var selectData:* = _dataList.length > index?_dataList[index]:"";
			var tmpLab	:Label=new Label();
			if(selectData is String)
			{
				titleStr	=	selectData;
			}
			else
			{
				titleStr	=	selectData.label;
			}
			//列表超框字符处理
			tmpLab.htmlStrByBorder = titleStr;
			while(tmpLab.width>(_initWid-17))
			{
				titleStr=titleStr.substring(0,titleStr.length-1);
				tmpLab.htmlStrByBorder = titleStr;
			}
			_titleItem.label = titleStr;
			
			if(_layOut=="left")
			{
				_titleItem.alignLeft();
			}
		}
		/**
		 *是否隱藏下拉列表 
		 * @param e
		 * 
		 */		
		private function showOrHideList(e:Event):void
		{
			if(_listContainer.visible){
				hideList();
			}else{
				showList();
			}
			dispatchEvent(new ComboBoxEvent(ComboBoxEvent.COMBOBOX_CLICK,{}));
		}
		/**
		 *更新显示框的数据 
		 * @param e
		 * updataTitle
		 */
		private function updataTitle(e:ListEvent):void
		{
			
			_titleItem.label = e.item.label;
			_indexItem=e.item.id;
//			if(_indexItem==e.item.id)
//			{
//				_titleItem.label = e.item.label;
//				//trace("是否加载成功"+e.item.label);
//			}
			var nxComboBoxEvt:ComboBoxEvent = new ComboBoxEvent(ComboBoxEvent.NXCOMBOBOX_CLICK,e.item);
			dispatchEvent(nxComboBoxEvt);
		}
		
		/**
		 *关闭下拉列表 
		 * 
		 */		
		private function hideList(e:Event=null):void
		{
			_listContainer.mouseChildren = _listContainer.mouseEnabled = _listContainer.visible = false;
			removeEventListener(MouseEvent.CLICK, onHideList);
		}
		private function showList():void
		{
			if(!_titleItem.enabled)
			{
				return;
			}
			_listContainer.mouseChildren = _listContainer.mouseEnabled = _listContainer.visible = true;
			addEventListener(MouseEvent.CLICK, onHideList);
		}	
//		public function getSelecedItem():Object
//		{
//			for each(var item:Object in _dataList){
//				if(item["label"] == title)
//					return item;				
//			}
//			return null;
//		}
		
		private function onHideList(e:MouseEvent):void
		{
			if(_titleItem.contains(e.target as DisplayObject) || (e.target is SimpleButton))
			{
				return;
			}
			hideList();	
		}
		
		public function get title():String
		{
			return _titleItem.label;
		}
		
		public function set title(title:String):void
		{
			_titleItem.label = title;
		}
		
		
		/**
		 * 给combobox添加选项 
		 * @param item
		 * 
		 */
		public function addItem(item:Object):void
		{
			_dataList.push(item);
			var retItem:Object = {label:item["label"],height:_listHeight,skin:_listSkin,data:item["data"]}
			_list.addObj(retItem);
		}
		
		/**
		 * 清除上次的
		 * 显示内容
		 */		
		public function clear():void
		{
			_titleItem.label = "";
			dataList = [];
			_list.reSet();
		}
		/**
		 *  data格式:{label:"测试0",data:""} 
		 * @param dataList
		 * 
		 */
		public function set dataList(pList:Array):void
		{
			//转化成nxlist用的
			_list.listData = toListData(pList);
			initTitle(0);
		}
		public function get dataList():Array
		{
			return _dataList;
		}
		/**
		 * 设置显示的内容 
		 * @param val
		 * 
		 */
		public function setSelected(val:int):void
		{
			if(val < 0 )return;
			initTitle(val);
			index = val;
		}

		public function get index():int
		{
			return _indexItem;
		}

		public function set index(value:int):void
		{			
			if(value<0)
			{
				value	=	0;
			}
			_indexItem = value;
			_selectItem	=_list.getItem(value) as ListItem;
			if(_selectItem)
			{
				title	=	_selectItem.label;
			}
			initTitle(value);
		}
		
		public function setListItemEnable(_index:uint,_mouseEnable:Boolean=true):void
		{
			var curListItem:ListItem = _list.getItem(_index) as ListItem;
			curListItem.enabled = _mouseEnable;
		}
		
		public function get list():List
		{
			return _list;
		}
		
		public function set layOut(val:String):void
		{
			_layOut = val;
			_list.layOut = val;
		}
		
		public function get layOut():String
		{
			return _layOut;
		}
		
		/**
		 * 是否可用 
		 * @param val
		 * 
		 */
		public function set canUse(val:Boolean):void
		{
			_titleItem.enabled = val;
		}
	}
}